from mongoengine import *
from bson import ObjectId
import json


# connect('sbo',username='kobako', password='qwer1234', authentication_source='admin')

class User(Document):
  name = StringField(required=True)
  pwd = StringField(required=True)
  nick = StringField(required=True) 
  def __str__(self):
      return "name:%s nick:%s pass:%s" % (self.name,self.nick,self.pwd)

class Player(Document):
    _id = ObjectIdField(required=False)
    username = StringField(required=True)
    power = IntField(required=True)
    level = IntField(required=True)
    name = StringField(required=True)
    growth_factor = IntField(required=True)
    dead_time = IntField(required=True)
    exp = IntField(required=True)
    hp = IntField(required=True)
    max_hp = IntField(required=True)
    area_id = ObjectIdField(required=True)
    online = BooleanField(required=True)
    def dict(self):
      return {'id':str(self._id), 'username': self.username,'power': self.power,'level': self.level,'exp': self.exp, 'area_id': str(self.area_id), 'growth_factor': self.growth_factor,'name': self.name, 'hp': self.hp, 'max_hp': self.max_hp, 'dead_time': self.dead_time}

class Area(Document):
  _id = ObjectIdField(required=True)
  name = StringField(required=True)
  pid = ObjectIdField(required=False, default='')
  def dict(self):
    return {'id': str(self._id) , 'name': self.name, 'pid': str(self.pid)}

class Mob(Document):
  _id = ObjectIdField(required=True)
  name = StringField(required=True)
  level = IntField(required=True)
  power = IntField(required=True)
  hp = IntField(required=True)
  growth_factor = IntField(required=True)
  revive_time = IntField(required=True)
  max_level = IntField(required=True)
  life_span = IntField(required=True)
  upgrade_time = IntField(required=True)
  desc = StringField(required=True)
  living_time = IntField(required=True)
  dead_time = IntField(required=True)
  tid = ObjectIdField(required=True)
  area_id = ObjectIdField(required=True)
  def dict(self):
    return {'id': str(self._id), 'name': self.name, 'power': self.power, 'level': self.level,'hp': self.hp, 'growth_factor': self.growth_factor, 'revive_time': self.revive_time, 'max_level': self.max_level, 'life_span': self.life_span, 'upgrade_time': self.upgrade_time, 'desc': self.desc, 'living_time': self.living_time, 'area_id': str(self.area_id), 'dead_time': self.dead_time}

class MobType(Document):
  _id = ObjectIdField(required=True)
  name = StringField(required=True)
  power = IntField(required=True)
  hp = IntField(required=True)
  growth_factor = IntField(required=True)
  revive_time = IntField(required=True)
  max_level = IntField(required=True)
  life_span = IntField(required=True)
  upgrade_time = IntField(required=True)
  desc = StringField(required=True)
  def dict(self):
    return {'id': str(self._id), 'name': self.name, 'power': self.power,'hp': self.hp, 'growth_factor': self.growth_factor, 'revive_time': self.revive_time, 'max_level': self.max_level, 'life_span': self.life_span, 'upgrade_time': self.upgrade_time, 'desc': self.desc}

class Inventory(Document):
  _id = ObjectIdField(required=True)
  player_id = ObjectIdField(required=True)
  thing_id = ObjectIdField(required=True)
  type = StringField(required=True)
  num = IntField(required=True)
  def dict(self):
    return {'id': str(self._id) , 'player_id': str(self.player_id), 'thing_id': str(self.thing_id), 'type': self.type, 'num': self.num}

class Item(Document):
  _id = ObjectIdField(required=True)
  name = StringField(required=True)
  desc = StringField(required=True)
  price = IntField(required=True)
  def dict(self):
    return {'id': str(self._id) , 'name': self.name, 'desc': self.desc, 'price': self.price}

class Equip(Document):
  _id = ObjectIdField(required=True)
  name = StringField(required=True)
  desc = StringField(required=True)
  part = StringField(required=True)
  price = IntField(required=True)
  hp = IntField(required=True)
  power = IntField(required=True)
  def dict(self):
    return {'id': str(self._id) , 'name': self.name, 'desc': self.desc, 'part': self.part, 'price': self.price, 'hp': self.hp, 'power': self.power}

class Drop(Document):
  _id = ObjectIdField(required=True)
  mobtype_id = ObjectIdField(required=True)
  thing_id = ObjectIdField(required=True)
  type = StringField(required=True)
  rate = IntField(required=True)
  num = IntField(required=True)
  def dict(self):
    return {'id': str(self._id) , 'mobtype_id': str(self.mobtype_id), 'thing_id': str(self.thing_id), 'type': self.type, 'rate': self.rate, 'num': self.num}


class Equiping(Document):
  _id = ObjectIdField(required=True)
  equip_id = ObjectIdField(required=True)
  player_id = ObjectIdField(required=True)
  def dict(self):
    return {'id': str(self._id) , 'equip_id': str(self.equip_id), 'player_id': str(self.player_id)}